const {
	creatOrUpdate,
	findCarts,
	updateCarts,
	removeCart,
	modifySelected
} = require('../service/cart.service')

class CartController{
	async addCart(ctx){
		const user_id = ctx.state.user.id
		const food_id = ctx.request.query.id * 1
		const res = await creatOrUpdate(user_id,food_id)
		ctx.body = {
			res
		}
	}

	async findAll(ctx){
		const {pageNum,pageSize} = ctx.request.query
		console.log(pageNum,pageSize);
		const res = await findCarts(pageNum,pageSize)
		ctx.body = {
			result:res
		}
	}

	async updateCart(ctx){
		const {number,selected} = ctx.request.body
		const id = ctx.request.query.id

		const res = await updateCarts({number,selected},id)
		if(res>0){
			ctx.body = {
				code:0,
				message:"修改购物车成功"
			}
		}
		
	}

	async destoryCart(ctx){
		const id = ctx.request.body.id

		const res = await removeCart(id)
		if(res>0){
			ctx.body = {
				code:0,
				message:"删除成功"
			}
		}
		
	}

	async allOrNotAll(ctx){
		const status = ctx.request.body.status
		const res = await modifySelected(status)
		ctx.body = {
			code:0,
			message:"全选或全不选修改成功"
		}
	}
}
module.exports = new CartController()